package com.hunttown.mes.impl;

import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.convert.AnalysisManageLoginLogConverts;
import com.hunttown.mes.rpc.api.AnalysisManageLoginLogRpcService;
import com.hunttown.mes.rpc.domain.query.AnalysisManageLoginLogDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.domain.AnalysisManageLoginLogDTO;
import com.hunttown.mes.service.AnalysisManageLoginLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisManageLoginLogConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:05:28
 */
@Service
public class AnalysisManageLoginLogRpcServiceImpl implements AnalysisManageLoginLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisManageLoginLogRpcServiceImpl.class);

    private final AnalysisManageLoginLogService analysisManageLoginLogService;

    @Autowired
    public AnalysisManageLoginLogRpcServiceImpl(AnalysisManageLoginLogService analysisManageLoginLogService) {
        this.analysisManageLoginLogService = analysisManageLoginLogService;
    }

    @Override
    public AnalysisManageLoginLogDTO insert(AnalysisManageLoginLogDTO analysisManageLoginLogDTO) {
        try {
            return convert(analysisManageLoginLogService.insert(AnalysisManageLoginLogConverts.convert(analysisManageLoginLogDTO)));
        } catch (Exception e) {
            logger.error("AnalysisManageLoginLog create failed.", e);
            throw new RuntimeException("AnalysisManageLoginLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisManageLoginLogDTO analysisManageLoginLogDTO) {
        try {
            return analysisManageLoginLogService.updateInfoById(AnalysisManageLoginLogConverts.convert(analysisManageLoginLogDTO));
        } catch (Exception e) {
            logger.error("AnalysisManageLoginLog update by id failed.", e);
            throw new RuntimeException("AnalysisManageLoginLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisManageLoginLogDTOQuery analysisManageLoginLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageLoginLogDTOQuery.getW_id() != null && analysisManageLoginLogDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisManageLoginLogDTOQuery.getW_id());
            }

            if (analysisManageLoginLogDTOQuery.getIds() != null && !analysisManageLoginLogDTOQuery.getIds().equals("")) {
                query.put("ids", analysisManageLoginLogDTOQuery.getIds());
            }

            if (analysisManageLoginLogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisManageLoginLogDTOQuery.getDeleteFlag());
            }

            return analysisManageLoginLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisManageLoginLog update by query failed.", e);
            throw new RuntimeException("AnalysisManageLoginLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageLoginLogDTO selectById(Integer id) {
        try {
            return convert(analysisManageLoginLogService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisManageLoginLog select by id failed.", e);
            throw new RuntimeException("AnalysisManageLoginLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageLoginLogDTO selectObjByQuery(AnalysisManageLoginLogDTOQuery analysisManageLoginLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisManageLoginLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisManageLoginLog select obj by query failed.", e);
            throw new RuntimeException("AnalysisManageLoginLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisManageLoginLogDTO> getListForPage(AnalysisManageLoginLogDTOQuery analysisManageLoginLogDTOQuery) {
        Query query = Query.page(analysisManageLoginLogDTOQuery.getPn(), analysisManageLoginLogDTOQuery.getPageSize());

        if (analysisManageLoginLogDTOQuery.getIds() != null && !analysisManageLoginLogDTOQuery.getIds().equals("")) {
            query.put("ids", analysisManageLoginLogDTOQuery.getIds());
        }

        if (analysisManageLoginLogDTOQuery.getClientType() != null && analysisManageLoginLogDTOQuery.getClientType() != -9999) {
            query.put("clientType", analysisManageLoginLogDTOQuery.getClientType());
        }

        if (analysisManageLoginLogDTOQuery.getAdminId() != null && analysisManageLoginLogDTOQuery.getAdminId() > 0) {
            query.put("adminId", analysisManageLoginLogDTOQuery.getAdminId());
        }

        if (analysisManageLoginLogDTOQuery.getLikeName() != null && !analysisManageLoginLogDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisManageLoginLogDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(analysisManageLoginLogDTOQuery.getShowDate())) {
            query.put("showDate", analysisManageLoginLogDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(analysisManageLoginLogDTOQuery.getStartDate())) {
            query.put("startDate", analysisManageLoginLogDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(analysisManageLoginLogDTOQuery.getEndDate())) {
            query.put("endDate", analysisManageLoginLogDTOQuery.getEndDate());
        }

        if (analysisManageLoginLogDTOQuery.getOrderBy() != null && analysisManageLoginLogDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisManageLoginLogDTOQuery.getOrderBy());
        }

        //区间日期
        if (StringUtils.isNotBlank(analysisManageLoginLogDTOQuery.getDaterange())) {
            String[] date = analysisManageLoginLogDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        List<AnalysisManageLoginLogDTO> list = convert(analysisManageLoginLogService.selectByQuery(query));
        Page<AnalysisManageLoginLogDTO> page = Page.from(analysisManageLoginLogDTOQuery.getPn(), analysisManageLoginLogDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisManageLoginLogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisManageLoginLogService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisManageLoginLog delete failed.", e);
            throw new RuntimeException("AnalysisManageLoginLog delete failed, Please order your code.", e);
        }
    }
}